Completed
Push — master ( 4e1210...2759f8 )
by Muhammad Dyas
16s queued 13s
created

handlers.js ➔ createMessageFromNameListHandler   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
rs 9.7
c 0
b 0
f 0
cc 1
1
import {buildMessageBody, buildNameListSection, buildNameListWinnerSection} from './src/helpers/components.js';
2
import {createMessage, updateMessage} from './src/helpers/api.js';
3
import {getRandomWinners} from './src/helpers/utils.js';
4
import {delayUpdateMessage} from './src/helpers/task.js';
5
6
/**
7
 * @param {object} requestBody - list of names
8
 * @returns {Promise<void>} update message
9
 */
10
export async function updateWinnerCardHandler(requestBody) {
11
  const names = requestBody.names;
12
  const winner = getRandomWinners(names);
13
  const messageText = `Congratulations! We have shuffled the list of names and the winner is *${winner.join(',')}*.`;
14
  const message = buildMessageBody(buildNameListWinnerSection(names, winner), messageText);
15
  const request = {
16
    name: requestBody.messageId,
17
    requestBody: message,
18
    updateMask: 'text,cardsV2',
19
  };
20
  await updateMessage(request);
21
}
22
23
/**
24
 * @param {array} names - list of name that will be shuffled
25
 * @param {string} space - google chat space name
26
 * @param {string} thread - chat thread/parent
27
 * @returns {Promise<void>} will post the message to google API
28
 */
29
export async function createMessageFromNameListHandler(names, space, thread = null) {
30
  const cardSection = buildNameListSection(names);
31
  const message = buildMessageBody(cardSection);
32
33
  const request = {
34
    parent: space,
35
    threadKey: thread,
36
    requestBody: message,
37
    messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
38
  };
39
  const apiResponse = await createMessage(request);
40
41
  const messageId = apiResponse.data.name;
42
  const payload = {
43
    messageId,
44
    names: names,
45
  };
46
  await delayUpdateMessage(JSON.stringify(payload));
47
}
48